package com.example.myapplication.ui.mine

import android.content.Intent
import android.os.Bundle
import android.view.*
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import com.example.myapplication.R
import kotlinx.android.synthetic.main.fragment_mine.*
import okhttp3.OkHttpClient
import okhttp3.Request
import org.json.JSONArray
import java.lang.Exception
import java.lang.StringBuilder
import kotlin.concurrent.thread


class MineFragment : Fragment() {

    private lateinit var mineViewModel: MineViewModel
    val sever_ip = "192.168.43.8"
    val CalcWantedMovieNumber_jsp = "http://${sever_ip}:8080/JspForAndroid/CalcWantedMovieNumber.jsp"
    //val CalcWantedMovieNumber_jsp = "http://${resources.getString(R.string.sever_ip)}:8080/JspForAndroid/CalcWantedMovieNumber.jsp"
    var number = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setHasOptionsMenu(true)
    }

    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        mineViewModel =
                ViewModelProviders.of(this).get(MineViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_mine, container, false)

        return root
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        super.onCreateOptionsMenu(menu, inflater)
        menu.add("Menu 1a").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        txtLogin.text = "张三"

        PersonalHomepage.setOnClickListener {
            val intent = Intent(activity, personalHomepage::class.java)
            startActivity(intent)
        }

        swipeRefresh.setOnRefreshListener {
            swipeRefresh.isRefreshing = true
            Toast.makeText(context, "refresh", Toast.LENGTH_LONG).show()
            swipeRefresh.isRefreshing = false

        }
        order_movie.setOnClickListener {
            val intent = Intent(activity, Order_movie_list::class.java)//电影
            startActivityForResult(intent, 5)
        }

        WantWatch.setOnClickListener {
            val intent = Intent(activity, WangToWatchActivity::class.java)//电影
            startActivityForResult(intent, 6)
        }

        getAllTrainInfoJson()
    }

    private fun getAllTrainInfoJson(){
        thread {
            try {
                val client = OkHttpClient()

                //构建请求
                val request = Request.Builder()
                    .url(CalcWantedMovieNumber_jsp)
                    .build()
                //执行
                val response= client.newCall(request).execute()
                //得到返回值
                val responseData = response.body?.string()
                //txtData.text = responseData
                val result = parseJson(responseData)
                if (responseData != null){
                    activity?.runOnUiThread{
                        if (number == "0")
                            wantedMovie_number.text = "-"
                        else
                            wantedMovie_number.text = number
                    }
                }
            }catch (e: Exception){
                e.printStackTrace()
            }
        }
    }
    private fun parseJson(jsonData:String?):String{
        val builder = StringBuilder()
        try {
            //json数组

            val jsonArray = JSONArray(jsonData)
            //清空原来的子项
            //向列表中加入子项
            for (i in 0 until jsonArray.length()){
                //取出元素
                val jsonObject = jsonArray.getJSONObject(i)
                number = jsonObject.getString("number")
            }
        }catch (e: Exception){
            e.printStackTrace()
        }

        return builder.toString()
    }
}

